#include "linked_list.h"
#include <stdlib.h>
#include <string.h>

// 手动实现 strdup 函数（如果编译器不支持）
#ifndef HAVE_STRDUP
char *strdup(const char *s) {
    char *p = (char *)malloc(strlen(s) + 1);
    if (p) {
        strcpy(p, s);
    }
    return p;
}
#endif

// 创建一个链表
void ListCreate(ListHead *list) {
    list->head = NULL;
    list->count = 0;
}

// 向链表尾部添加数据
void ListPushBack(ListHead *list, void *data, const char *name) {
    ListNode *new_node = (ListNode *)malloc(sizeof(ListNode));
    if (new_node == NULL) {
        return; // 内存分配失败
    }
    new_node->data = data;
    new_node->name = strdup(name); // 复制名称
    new_node->next = NULL;

    if (list->head == NULL) {
        list->head = new_node;
    } else {
        ListNode *current = list->head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_node;
    }
    list->count++;
}

// 向链表头部添加数据
void ListPushFront(ListHead *list, void *data, const char *name) {
    ListNode *new_node = (ListNode *)malloc(sizeof(ListNode));
    if (new_node == NULL) {
        return; // 内存分配失败
    }
    new_node->data = data;
    new_node->name = strdup(name); // 复制名称
    new_node->next = list->head;
    list->head = new_node;
    list->count++;
}

// 按位置插入数据
void ListInsert(ListHead *list, size_t position, void *data, const char *name) {
    if (position == 0) {
        ListPushFront(list, data, name);
        return;
    }

    ListNode *current = list->head;
    for (size_t i = 0; i < position - 1 && current != NULL; i++) {
        current = current->next;
    }

    if (current == NULL) {
        return; // 位置超出范围
    }

    ListNode *new_node = (ListNode *)malloc(sizeof(ListNode));
    if (new_node == NULL) {
        return; // 内存分配失败
    }
    new_node->data = data;
    new_node->name = strdup(name); // 复制名称
    new_node->next = current->next;
    current->next = new_node;
    list->count++;
}

// 给定结点插入数据，插入到结点前
void ListInsertForNode(ListHead *list, ListNode *node, void *data, const char *name) {
    if (node == list->head) {
        ListPushFront(list, data, name);
        return;
    }

    ListNode *current = list->head;
    while (current != NULL && current->next != node) {
        current = current->next;
    }

    if (current == NULL) {
        return; // 结点未找到
    }

    ListNode *new_node = (ListNode *)malloc(sizeof(ListNode));
    if (new_node == NULL) {
        return; // 内存分配失败
    }
    new_node->data = data;
    new_node->name = strdup(name); // 复制名称
    new_node->next = node;
    current->next = new_node;
    list->count++;
}

// 从链表尾部删除数据
void ListEraseBack(ListHead *list) {
    if (list->head == NULL) {
        return; // 链表为空
    }

    if (list->head->next == NULL) {
        free(list->head->name); // 释放名称
        free(list->head);
        list->head = NULL;
        list->count--;
        return;
    }

    ListNode *current = list->head;
    while (current->next->next != NULL) {
        current = current->next;
    }
    free(current->next->name); // 释放名称
    free(current->next);
    current->next = NULL;
    list->count--;
}

// 从链表头部删除数据
void ListEraseFront(ListHead *list) {
    if (list->head == NULL) {
        return; // 链表为空
    }

    ListNode *temp = list->head;
    free(temp->name); // 释放名称
    list->head = list->head->next;
    free(temp);
    list->count--;
}

// 删除给定结点
void ListEraseForNode(ListHead *list, ListNode *node) {
    if (list->head == NULL || node == NULL) {
        return; // 链表为空或结点未找到
    }

    if (list->head == node) {
        ListEraseFront(list);
        return;
    }

    ListNode *current = list->head;
    while (current != NULL && current->next != node) {
        current = current->next;
    }

    if (current == NULL) {
        return; // 结点未找到
    }

    current->next = node->next;
    free(node->name); // 释放名称
    free(node);
    list->count--;
}

// 获取头结点
ListNode* ListGetHead(ListHead *list) {
    return list->head;
}

// 获取尾结点
ListNode* ListGetTail(ListHead *list) {
    if (list->head == NULL) {
        return NULL;
    }

    ListNode *current = list->head;
    while (current->next != NULL) {
        current = current->next;
    }
    return current;
}

// 获取指定位置的结点
ListNode* ListGetNode(ListHead *list, size_t position) {
    if (list->head == NULL) {
        return NULL;
    }

    ListNode *current = list->head;
    for (size_t i = 0; i < position && current != NULL; i++) {
        current = current->next;
    }

    return current;
}

// 获取指定结点的下一个结点
ListNode* ListGetNodeNext(ListNode *node) {
    return node ? node->next : NULL;
}

// 销毁链表
void ListDestroy(ListHead *list) {
    ListNode *current = list->head;
    while (current != NULL) {
        ListNode *temp = current;
        current = current->next;
        free(temp->name); // 释放名称
        free(temp);
    }
    list->head = NULL;
    list->count = 0;
}